package com.boyaa.shells.tool 
{
	import com.boyaa.debug.Debug;
	import com.boyaa.fly.Timepiece;
	import com.boyaa.shells.control.ButtonManager;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author ...
	 */
	public class ScrollList
	{
		private var _up:MovieClip = null;
		private var _down:MovieClip = null;
		//private var _bar:MovieClip = null;
		private var _type:String = "";
		public var _top:int = 17;
		public var _length:int = 116;
		private var _callBack:Function = null;
		private var _num:int = 0;
		private var _start:int = 0;
		private var _end:int = 0;
		private var _x:int = 0;
		private var _timer:Timer = null;
		private var _timeNum:int = 0;
		
		public function ScrollList(mc:MovieClip, fun:Function = null) 
		{
			_up = mc.right;
			_down = mc.left;
			_callBack = fun;
		}
		
		
		public function addEvent():void
		{
			_up.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_up.addEventListener(MouseEvent.MOUSE_UP, onUp);
			_down.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_down.addEventListener(MouseEvent.MOUSE_UP, onUp);
			
			_up.addEventListener(MouseEvent.ROLL_OUT, onUp);
			_down.addEventListener(MouseEvent.ROLL_OUT, onUp);	
			ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _up);
			ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _down);
		}
		
		public function removeEvent():void
		{
			_up.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_up.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			_down.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_down.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			
			_up.removeEventListener(MouseEvent.ROLL_OUT, onUp);
			_down.removeEventListener(MouseEvent.ROLL_OUT, onUp);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _up);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _down);
		}
		
		
		public function setScale(n:Number,s:Number,e:Number):void
		{
			_num = n;
			_start = s;
			_end = e;
		}
		
		private function onUp(e:MouseEvent):void 
		{
			Timepiece.instance.removeFun(onMove);
			Timepiece.instance.removeFun(ontime, Timepiece.TIMER);
			var n:int = 0;
			if (_type == "up" && _x % _num != 0)
			{
				if (_x == _end || (_x - _num) < _end) 
					n = _end - _x ;
				else
					n = int( _x / _num -1) * _num -_x;
			}
			else
			{
				if (_x % _num != 0)
				{
					if (_x == _start || (_x + _num) > _start) 
						n = - _x ;
					else
						n = int( _x / _num + 1) * _num -_x;
				}
			}
			_x += n;
			_callBack(n);
		}
		
		private function onDown(e:MouseEvent):void 
		{
			if (e.currentTarget == _up)
				_type = "up";
			else
				_type = "down";
			//_timer.start();
			onMove1();
			Timepiece.instance.addTimerFun(ontime,500);
		}
		
		private function ontime():void
		{
			Timepiece.instance.addFun(onMove);
			Timepiece.instance.removeFun(ontime, Timepiece.TIMER);
		}
		private function onMove1():void
		{
			var n:int = 0;
			if (_type == "up")
			{
				if (_x < _end)
					return;
				if (_x == _end || (_x - _num) <= _end) 
					n = _end - _x ;
				else
					n = -_num;
			}
			else
			{
				if (_x > _start)
					return;
				if (_x == _start || (_x + _num) >= _start) 
					n = - _x ;
				else
					n =  Math.abs(_num);
			}
			_x += n; 
			_callBack(n);
		}
		
		private function onMove():void
		{
			var n:int = 0;
			if (_type == "up")
			{
				if (_x < _end)
					return;
				if (_x == _end || (_x - _num/5) <= _end) 
					n = _end - _x ;
				else
					n = -_num/5;
			}
			else
			{
				if (_x > _start)
					return;
				if (_x == _start  || (_x + _num/5) >= _start) 
					n = - _x ;
				else
					n =  Math.abs(_num/5);
			}
			_x += n; 
			_callBack(n);
		}
	}

}